package com.example.controller.buyer;

import com.example.base.BaseController;
import com.example.entity.User;
import com.example.service.UserService;
import com.example.vo.IdHolder;
import com.example.vo.Result;
import com.example.vo.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author 朽
 */
@RestController
@Slf4j
@RequestMapping("/buyer/userInfo")
public class UserInfoController extends BaseController<UserService, User> {
    @GetMapping("/getUserInfo")
    @Operation(summary = "获取用户信息")
    public Result<UserInfoVo> getUserInfo(){
        Long userId = IdHolder.getUserId();
        UserInfoVo userInfoVo = baseService.getUserInfo(userId);
        if(userInfoVo == null){
            return Result.error("获取用户信息失败");
        }
        return Result.ok(userInfoVo);
    }

    @PostMapping("/updateProfile")
    @Operation(summary = "修改用户头像")
    public Result<String> updateProfile(@RequestParam String avatar){
        Long userId = IdHolder.getUserId();
        boolean isUpdate = baseService.updateProfile(userId, avatar);
        if(!isUpdate){
            return Result.error("用户头像修改失败");
        }
        return Result.ok("用户头像修改成功");
    }

    @PostMapping("/updatePassword")
    @Operation(summary = "修改用户密码")
    public Result<String> updatePassword(@RequestParam String password, @RequestParam String comfirmPassword){
        Long userId = IdHolder.getUserId();
        if(!password.equals(comfirmPassword)){
            Result.error("两次密码输入不匹配");
        }
        boolean isUpdate = baseService.updatePassword(userId, password);
        if(!isUpdate){
            return Result.error("用户密码修改失败");
        }
        return Result.ok("用户密码修改成功");
    }

    @PostMapping("/updateUserInfo")
    @Operation(summary = "修改用户昵称")
    public Result<String> updateUserInfo(@RequestParam String nickname){
        Long userId = IdHolder.getUserId();
        boolean isUpdate = baseService.updateUserInfo(userId, nickname);
        if(!isUpdate){
            return Result.error("昵称修改失败");
        }
        return Result.ok("昵称修改成功");
    }
}
